<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache,no-store,must-revalidate">
    <meta http-equiv="Cache" content="no-cache">
		<title>新增页面</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="" lay-filter="form-filter">
			<div class="mainBox">
				<div class="main-container">

					<div class="layui-form-item">
						<label class="layui-form-label">账户名</label>
						<div class="layui-input-block">
							<input type="text" name="userCode" lay-verify="required" autocomplete="off" placeholder="请输入账户名"
								class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">用户姓名</label>
						<div class="layui-input-block">
							<input type="text" name="userFullName" lay-verify="required" autocomplete="off" placeholder="请输入用户姓名"
								class="layui-input">
						</div>
					</div>
					
					<div class="layui-form-item">
						<label class="layui-form-label">用户角色</label>
						<div class="layui-input-block">
							<input type="radio" name="roleID" value="11" title="管理员" lay-filter="belong2-filter" >
							<input type="radio" name="roleID" value="12" title="一般用户" lay-filter="belong2-filter" checked>
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">所属制造部</label>
						<div class="layui-input-block">
							<select name="belong2Top" lay-verify="required">
								<option value=""></option>
							</select>
						</div>
					</div>
	
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="userAdd">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
	
		<script type="text/javascript">
			// 加载自定义模块,检查浏览器和token
			layui.extend({
        myheader: "{/}/lay-ext/myheader",
      });
			
			
			layui.use(['form', 'jquery','myheader','layer','laytpl','select'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let myheader = layui.myheader;
				let layer = layui.layer;
				let laytpl = layui.laytpl;
				let select = layui.select;
				
				var optionData={
					secdlist:[],
					macnolist:[]
				}
				// 定义全局 ajax的请求头
				myheader.ajaxGlobal(myheader.checkToken().tokenValue);

				// 获取select 数据
				$.ajax(parent.layui.myheader.baseUrl()+"/topDept/getAllTopDeptName",{
					async:false, // 同步方法
					success: function(res){
						if(res.data.length>0){
							optionData.list =[...res.data]
						} else {
							parent.layer.msg('未获取有效数据')
						}
					}
				})
				
				// 填充layui模板
				let getTpl = optionTpl.innerHTML
				laytpl(getTpl).render(optionData.list,function(html){
					$("[name='belong2Top']").html(html)
					// form重新渲染select
					form.render('select');
				})


				$("[type='reset']").click(function(){
					location.reload()
				})

				form.on('submit(userAdd)',function(data){

					$.ajax({
						url: parent.layui.myheader.baseUrl()+ '/user/addUser',
						// data: data.field,
						data:JSON.stringify({
							user:{
								userCode:data.field.userCode,
								userFullName: data.field.userFullName,
								roleID:data.field.roleID
							},
							topDept:{
								departmentID:data.field.belong2Top
							}
						}),
						contentType: 'application/json',		// 请求时的数据类型
						type: 'post',
						success: function(data) {
							if (data.data==1) {
								layer.msg("已添加", {
									time: 1000
								}, function() {
									parent.layer.close(parent.layer.getFrameIndex(window
										.name)); //关闭当前页
									parent.layui.table.reloadData('user-table',{
										scrollPos: 'fixed'  // 保持滚动条位置不变 - v2.7.3 新增
									});
								});
							} else {
								layer.msg("添加失败", {
									time: 1000
								});
							}
						}
					})
					return false
				})
				
			})
		</script>
				
		<script id="optionTpl" type="text/html">
			<option value=""></option>
			{{# layui.each(d,function(index,item){ }}
				<option value="{{item.topDeptID}}">{{item.topDeptName}} </option>
			{{# }); }}
		</script>
		
	</body>
</html>
